<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/plugins/bootstrap-4.1.1-dist/css/bootstrap.min.css">
		<script src="/js/plugins/jquery/jquery.min.js"></script>
		<script src="/js/plugins/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="/js/plugins/jrender/jrender.min.js"></script>
		<script src="/js/plugins/form/jquery.form.js"></script>
		<script>
            $(function () {
                //获取当前用户
                var user = JSON.parse(sessionStorage.getItem("user"));
                //渲染页面
                $('.info').renderValues(user);
                //添加背景图片的样式	background-image: url('user.coverImgUrl');
                $('.myBg').css("background-image",'url(' + user.coverImgUrl + ')');
				//设置表单的action提交路径
				$('#infoForm').attr("action","/users/"+user.id);
				//保存按钮
				$('#saveBtn').click(function () {
					$('#infoForm').ajaxSubmit(function (data) {
						if(data.success){
                            //把更新的用户重新放到sessionStorage中
                            sessionStorage.setItem("user",JSON.stringify(data.result));
						    window.location.href="/mine/profiles.html";
						}
                    });
                });
				//标记当前的操作是修改头像还是背景
				var flag;
                //编辑头像
                $("#headBtn").click(function () {
					//触发文件的点击动作
					$('#file').click();
					flag = true;
                });
                //编辑背景封面
                $("#coverBtn").click(function () {
					//触发文件的点击动作
					$('#file').click();
					flag = false;
                });
				//文本框的change事件
				$('#file').change(function () {
				    //如果有值才提交
					if($(this).val()){
					    //提交到上传的接口
						$('#fileFrom').ajaxSubmit(function (data) {
							//如果有值才提交
							if(data.status==1){
							    if(flag){
                                    //回显头像
                                    $("#headImg").attr("src",data.url);
                                    //设置到隐藏域中
									$('input[name=headImgUrl]').val(data.url);
								}else{
                                    //回显背景封面
                                    $('.myBg').css("background-image",'url(' + data.url + ')');
                                    //设置隐藏域
                                    $('input[name=coverImgUrl]').val(data.url);
								}

							}
						});
					};
				});
            });
		</script>
		
	</head>

	<body>

		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="#" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>
		<form id="fileFrom" action="/images" method="post" enctype="multipart/form-data">
			<input type="file" id="file" name="file" style="display: none">
		</form>
		<div class="container info">
			<div class="myBg">
				<img id="headImg"  class="rounded-circle" render-src="headImgUrl"/>
				<div class="editHead" id="headBtn" ><i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i></div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<form id="infoForm" method="post">
					<input type="hidden" name="headImgUrl" render-value="headImgUrl">
					<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">
					<input type="hidden" name="email" render-value="email"><!--用于做回显,不进行更新-->
					<input type="hidden" name="_method" value="PUT"><!--因为是更新,所以要改成put请求方式-->


					<label>昵称</label><input type="text" name="nickName" class="form-control" render-value="nickName">
					<label>性别</label><select class="form-control" name="gender" render-value="gender">
							<option value="-1">保密</option>
							<option value="1">男</option>
							<option value="0">女</option>
						</select>
					<label>地区</label><input type="text" name="place"  class="form-control" render-value="place">
					<label>签名</label><textarea name="sign" class="form-control" rows="5" render-html="sign"></textarea>
			</form>
		</div>
		
		
		
	</body>
</html>
